package Curriculums;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author Cesar
 */
import java.sql.*;

public class Profesor {
    static String bd = "bitacoraelectronica";
    static String login = "root";
    static String password = "A74543";
    static String url = "jdbc:mysql://localhost/"+bd;
    
    Connection conn = null;
            
    public Profesor(){
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection(url,login,password);
            if (conn != null) {
                System.out.println("Conexión a base de datos "+url+" ... Ok");
            }
        }
        catch (Exception e) {
                System.out.println(e);
                System.exit(0);
        }
        
    }
    
    public String getNombre(String usuario){
        String res = "";
        try{
            Statement s = conn.createStatement();
            ResultSet rs = s.executeQuery("select Nombre from usuario where"
                    + " username ='"+usuario+"'");
            while (rs.next())
            {
                res =  rs.getString("Nombre");
            }
        }
        catch (Exception e) {
                System.out.println(e);
                System.exit(0);
        }
        return res;
    }
    
    public String getTelefono(String usuario){
        String res = "";
        try{
            Statement s = conn.createStatement();
            ResultSet rs = s.executeQuery("select Telefono from usuario where"
                    + " username ='"+usuario+"'");
            while (rs.next())
            {
                res =  rs.getString("Telefono");
            }
        }
        catch (Exception e) {
                System.out.println(e);
                System.exit(0);
        }
        return res;
    }
    
    public String getNacionalidad(String usuario){
        String res = "";
        try{
            Statement s = conn.createStatement();
            ResultSet rs = s.executeQuery("select Nacionalidad from usuario"
                    + " where username ='"+usuario+"'");
            while (rs.next())
            {
                res =  rs.getString("Nacionalidad");
            }
        }
        catch (Exception e) {
                System.out.println(e);
                System.exit(0);
        }
        return res;
    }
    
    public String getId(String usuario){
        String res = "";
        try{
            Statement s = conn.createStatement();
            ResultSet rs = s.executeQuery("select Identificacion from usuario"
                    + " where username ='"+usuario+"'");
            while (rs.next())
            {
                res =  rs.getString("Identificacion");
            }
        }
        catch (Exception e) {
                System.out.println(e);
                System.exit(0);
        }
        return res;
    }
    
    public String getUser(String nombre){
        String res = "";
        try{
            Statement s = conn.createStatement();
            ResultSet rs = s.executeQuery("select username from usuario"
                    + " where nombre ='"+nombre+"'");
            while (rs.next())
            {
                res =  rs.getString("Username");
            }
        }
        catch (Exception e) {
                System.out.println(e);
                System.exit(0);
        }
        return res;
    }
    
    public void insertarProfesor(String usuario, String nom, String pass, 
        String tel, String nac, String id){
        try{
            Statement s = conn.createStatement();
            s.executeUpdate("INSERT INTO usuario VALUES ('"+usuario+"','"+nom+""
                    + "','"+pass+"','"+tel+"','"+nac+"','"+id+"')");
            s.executeUpdate("INSERT INTO Profesor VALUES ('"+usuario+"')" );
        }
        catch (Exception e) {
            System.out.println(e);
            System.exit(0);
        }
    }
    
        public void actualizarProfesor(String usuario, String nom, String pass, 
            String tel, String nac, String id){
        try{
            Statement s = conn.createStatement();
            s.executeUpdate("UPDATE USUARIO SET Nombre = '"+nom+""
                    + "',Telefono = '"+tel+"',Nacionalidad = '"+nac+"',"
                    + "Identificacion = '"+id+"' WHERE Username = '"+usuario+"'");
           
        }
        catch (Exception e) {
            System.out.println(e);
            System.exit(0);
        }
    }
        
    public ResultSet profesores(){
        ResultSet rs = null;
        try{
            Statement s = conn.createStatement();
            rs = s.executeQuery("SELECT NOMBRE FROM USUARIO U, PROFESOR P"
                    + "WHERE U.USERNAME = P.USERNAME");
        }
        catch (Exception e) {
            System.out.println(e);
            System.exit(0);
        }
        return rs;
    }
    
    public void EliminarProfesor(String usuario){
        try{
            Statement s = conn.createStatement();
            s.executeUpdate("Delete From profesor where usernameProf = '"+usuario+"'");
            s.executeUpdate("Delete From usuario where username = '"+usuario+"' ");
           
        }
        catch (Exception e) {
            System.out.println(e);
            System.exit(0);
        }
    }
}
